<template>
	<view class="setting-container" :style="{height: `${windowHeight}px`}">
		<view class="menu-list">
			<view class="list-cell list-cell-arrow" @click="handleToPwd">
				<view class="menu-item-box">
					<view class="iconfont icon-password menu-icon" style="color: #ff3140;"></view>
					<view>修改密码</view>
				</view>
			</view>
			<view class="list-cell row" @click="handleToPhone">
				<view class="menu-item-box">
					<uni-icons type="phone" color="#ff3140"></uni-icons>
					<view class="" style="margin-left: 5px;">手机号：{{phonenumber}}</view>
				</view>
				<view class="btn-edit-p" @click="editPhone">
					更换手机号
				</view>
			</view>
			<!-- 			<view class="list-cell list-cell-arrow" @click="handleToUpgrade">
				<view class="menu-item-box">
					<view class="iconfont icon-refresh menu-icon"></view>
					<view>检查更新</view>
				</view>
			</view>
			<view class="list-cell list-cell-arrow" @click="handleCleanTmp">
				<view class="menu-item-box">
					<view class="iconfont icon-clean menu-icon"></view>
					<view>清理缓存</view>
				</view>
			</view> -->
		</view>
		<view class="cu-list menu">
			<view class="cu-item item-box">
				<view class="content text-center" @click="handleLogout">
					<text class="text-black">退出登录</text>
				</view>
			</view>
		</view>
		<u-popup :show="showPhone" mode="center" :round="15" @close="closePhone" @open="editPhone">
			<view class="modify-container column-center bg-white">
				<view class="title xl">修改手机号</view>
				<view class="modify-row row">
					<view style="width: 71px;">新手机号</view>
					<input v-model="newPhonenumber" type="number" maxlength="11" placeholder="请输入新的新手机号" />
				</view>
				<view class="btn bg-primary white row-center" @click="changeNameConfirm">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		getInfo
	} from '@/api/login';
	import {
		userBindPhoneApi,
		getPhoneApi
	} from '@/api/index.js';
	export default {
		data() {
			return {
				windowHeight: uni.getSystemInfoSync().windowHeight,
				phonenumber: '',
				newPhonenumber: '',
				showPhone: false,
				userId: ''
			}
		},
		onLoad() {
			this.getPhone();
			this.getUserInfo();
		},
		methods: {
			async getPhone() {
				const res = await getPhoneApi(uni.getStorageSync('UserInfo').userId);
				console.log(res, 'res');
				if (res.code != 200) return
				this.phonenumber = res.msg;
			},
			async changeNameConfirm() {
				if (!this.newPhonenumber) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					});
					return;
				}

				// 添加手机号格式校验
				const phoneReg = /^[1][3-9]\d{9}$/; // 正则：11位，以1开头，第二位为3-9 
				if (!phoneReg.test(this.newPhonenumber)) {
					uni.showToast({
						title: '手机号格式不正确',
						icon: 'none'
					});
					return;
				}
				const res = await userBindPhoneApi({
					userId: this.userId,
					phone: this.newPhonenumber
				});
				console.log(res, 'res');
				if (!res) return
				if (res.code === 200) {
					this.newPhonenumber = '';
					this.closePhone();
					this.getPhone();
				}
				uni.showToast({
					title: res.msg,
					icon: 'none'
				});
			},
			editPhone() {
				this.showPhone = true;
			},
			closePhone() {
				this.showPhone = false;
			},
			// 获取用户信息
			getUserInfo() {
				getInfo().then(res => {
					console.log(res.user, '获取用户信息');
					this.userId = res.user.userId;
				})
			},
			handleToPwd() {
				this.$tab.navigateTo('/pages/mine/pwd/index')
			},
			handleToUpgrade() {
				this.$modal.showToast('模块建设中~')
			},
			handleToPhone() {

			},
			handleLogout() {
				this.$modal.confirm('确定注销并退出系统吗？').then(() => {
					this.$store.dispatch('LogOut').then(() => {}).finally(() => {
						this.$tab.reLaunch('/pages/index')
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #f8f8f8;
	}

	.item-box {
		background-color: #FFFFFF;
		margin: 30rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		padding: 10rpx;
		border-radius: 8rpx;
		color: #303133;
		font-size: 32rpx;
	}

	.btn-edit-p {
		border-radius: 50rpx;
		min-width: 180rpx;
		padding: 10rpx 10rpx;
		border: 1rpx solid #FF2C3C;
		color: #FF2C3C;
		text-align: center;
	}

	.modify-container {
		padding-left: 30rpx;
		padding-right: 30rpx;
		padding-bottom: 30rpx;
		width: 580rpx;
		border-radius: 30rpx;
		background-color: $-color-white;

		.title {
			padding: 26rpx 0rpx;
		}

		.modify-row {
			padding: 32rpx 0rpx;
			width: 100%;
			border-bottom: 1rpx solid #E5E5E5;

			.send-code-btn {
				border: 1px solid $-color-primary;
				width: 184rpx;
				height: 62rpx;
				color: $-color-primary;
			}
		}

		.btn {
			height: 80rpx;
			padding: 0 180rpx;
			border-radius: 20rpx;
			margin-top: 60rpx;
		}
	}
</style>